package top.lyjwn.todo.common.template;

import com.alibaba.fastjson2.annotation.JSONField;

import java.util.ArrayList;
import java.util.List;

/**
 * 请求响应模板
 *
 * @author luoyujie
 */
public class R<T> {


    /**
     * 是否成功完成
     */
    @JSONField(ordinal = 1)
    protected boolean done;
    /**
     * 状态代码
     */
    @JSONField(ordinal = 2)
    protected Integer code;
    /**
     * 返回信息
     */
    @JSONField(ordinal = 3)
    protected String msg;

    /**
     * 对象数据
     */
    @JSONField(ordinal = 4)
    protected T data;

    /**
     * 列表数据
     */
    @JSONField(ordinal = 5)
    protected List<T> list = new ArrayList();

    /**
     * 列表总数
     */
    @JSONField(ordinal = 6)
    protected Long listTotal = 0L;


    public R() {
    }

    public R(boolean done) {
        this.done = done;
    }

    public R(boolean done, Integer code, String msg) {
        this.done = done;
        this.code = code;
        this.msg = msg;
    }

    public static R init() {
        return new R<>();
    }

    public static <M> R<M> init(Class<M> clazz) {
        return new R<>();
    }

    public static <M> R<M> init(Class<M> clazz, boolean done) {
        return new R<>(done);
    }

    public static <M> R<M> init(Class<M> clazz, boolean done, Integer code, String msg) {
        return new R<>(done, code, msg);
    }


    /**
     * 未登录
     */
    public R<T> notLogin() {
        return new R(false, 410, "未登录");
    }

    /**
     * 成功，默认状态码
     *
     * @param msg
     */
    public R<T> ok(String msg) {
        return new R(true, 200, msg);
    }

    /**
     * 失败
     *
     * @param code
     * @param msg
     * @return
     */
    public R<T> error(Integer code, String msg) {
        R r = new R(false, code, msg);
        r.listTotal = null;
        r.list = null;
        return r;
    }

    public boolean isDone() {
        return done;
    }

    public R<T> setDone(boolean done) {
        this.done = done;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public R<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Long getListTotal() {
        return listTotal;
    }

    public R<T> setListTotal(long listTotal) {
        this.listTotal = listTotal;
        return this;
    }

    public List<T> getList() {
        return list;
    }

    public R<T> setList(List<T> list) {
        this.list = list;
        return this;
    }
}
